package apiUtils.apis;

import android.content.Context;
import android.text.TextUtils;

import com.example.jhlibrarys.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import apiUtils.APISHelper;
import apiUtils.CallBack;
import apiUtils.models.VersionModel;
import apiUtils.params.DESParams;
import apiUtils.params.Params;
import appUtils.AppUtil;
import appUtils.PayTokenException;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

import static apiUtils.BaseAPIS.NETWORK_UNKNOWN;
import static constant.ApiConstants.DOMAIN_NAME;
import static constant.ApiConstants.SYSTEM_NAME;

/**
 * Created by lj on 2017/7/13 0013.
 */

public class GetVersionsAPI extends APISHelper {
    private static final String tag = "GetVersionsAPI";
    private static final String url = DOMAIN_NAME + "checkVersion";
    private final Params params = new DESParams();
    private final Context mContext;
    private String versions;


    public GetVersionsAPI(Context context) {
        this.mContext = context;
    }

    public void getVersionsAPI(String versions, final Listener listener) {

        this.versions = versions;
        try {
            post(tag, url, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        String decodeMsg = desDecode(object);
                        if (!TextUtils.isEmpty(decodeMsg)) {
                            VersionModel model = sGon.fromJson(decodeMsg, VersionModel.class);
                            success(model, listener);
                        } else {
                            failure(-1, mContext.getString(R.string.information_error), listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        PayTokenException.getInstance().saveExceptionFile("GetVersionsAPI" +
                                " getVersionsAPI Json error = " + e.getMessage());
                    }
                }

                @Override
                public void onError(int code) {
                    if (code == NETWORK_UNKNOWN){
                        failure(code, AppUtil.getInstance().getContext().getString(R.string.check_network), listener);
                    }else {
                        failure(code, "", listener);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            PayTokenException.getInstance().saveExceptionFile("GetVersionsAPI" +
                    " getVersionsAPI Json error = " + e.getMessage());
        }

    }

    private void success(final VersionModel model, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (null != listener) {
                    listener.onSuccess(model);
                }
            }
        });
    }

    private void failure(final int code, final String msg, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (listener != null) {
                    listener.onFailure(code, msg);
                }
            }
        });
    }

    @Override
    protected Params initParams() throws JSONException {
        params.put(mContext.getString(R.string.paramSystemName), SYSTEM_NAME);
        params.put("version", versions);
        return params;
    }

    public interface Listener {
        void onSuccess(VersionModel model);

        void onFailure(int code, String msg);
    }
}
